package org.mipt.infsec.steganography.utils;

import org.mipt.infsec.steganography.api.datastorage.Bit;

public class ConvertUtils
{
    public static Bit[] stringToBits(String data)
    {
        byte[] bytes = data.getBytes();

        Bit[] bits = new Bit[bytes.length * 8];

        for (int i = 0; i < bytes.length; i++)
        {
            Bit[] buf = Bit.getBits(bytes[i]);
            for (int j = 0; j < buf.length; j++)
            {
                bits[8*i + j] = buf[j];
            }
        }
        for (Bit bit : bits)
            System.out.print(bit);
        System.out.println();
        return bits;
    }

    public static String bitsToString(Bit[] bits) throws Exception
    {
         return new String(bitsToBytes(bits));
    }

    public static byte[] bitsToBytes(Bit[] bits) throws Exception
    {
        for (Bit bit : bits)
            System.out.print(bit);
        System.out.println();
        byte[] result = new byte[bits.length / 8];
        for (int i = 0; i < bits.length / 8; i++)
        {
            for (int j = 0; j < 8; j++)
                if (bits[8*i + j].isUNIT()) result[i] += (byte)Math.pow(2.0, (double)(7 - j));
        }
        return result;
    }

}
